<?php

/**
 *   OPENSHOP 管理中心商品零售数据统计
 * 
 *   @link        http://baison.com.cn
 *   @copyright   Baison, Inc.
 *   @package     OpenShop
 *   @version     $Id: OS_TJ.php,v 1.0 2009/09/21 08:36:58 modified $
 *   @author      Jiajun <jiajun@baison.com.cn>
 */

define('IN_OS', true);

require(dirname(__FILE__) . '/includes/init.php');

/*------------------------------------------------------ */
//-- 商品零售数据统计列表
/*------------------------------------------------------ */
if ($_REQUEST['act'] == 'list')
{ 
	
	$sql = 'select * from '.$os->table('shipping').' as s order by s.shipping_id asc';
	$shipping_list_info = $db->getAll($sql);
	$smarty->assign('shipping_list_info',   $shipping_list_info);

	$sql = 'select * from '.$os->table('payment').' as p order by p.pay_id asc';
	$pay_list_info = $db->getAll($sql);	
	$smarty->assign('pay_list_info',   $pay_list_info);	
	
    $smarty->assign('ur_here',      '商品零售数据统计');
    $smarty->assign('full_page',    1);

    $arr = array();
	$arr['pay_id']  		= intval($_POST['pay_info']);
	$arr['shipping_id']   = intval($_POST['shipping_info']);
	$arr['start_time']  	= trim($_POST['start_time']);
	$arr['end_time']  		= trim($_POST['end_time']);
    
    $sales_stats = get_sales_stats($arr);
 
    $smarty->assign('sales_stats',  $sales_stats['list']);
	$smarty->assign('number_sum',   $sales_stats['number_sum']);
	$smarty->assign('money_sum',    $sales_stats['money_sum']);

    assign_query_info(); 
    $smarty->display('OS_TJ.tpl');
}

function get_sales_stats($arr)
{
	$result = array();
	$number_sum = 0;
	$money_sum  = 0;
	
	$where  = 'where 1 ';
	
	if($arr['pay_id'] > 0){
		$where .= ' and oi.pay_id = '.$arr['pay_id'];
	}
	
	if($arr['shipping_id'] > 0){
		$where .= ' and oi.shipping_id = '.$arr['shipping_id'];
	}
	
	if($arr['start_time'] != '' && $arr['end_time'] != ''){
		$time_start = intval(strtotime($arr['start_time'])) - 8*60*60;
		$time_end   = intval(strtotime($arr['end_time'])) - 8*60*60;
		$where .= " AND oi.add_time > ".$time_start." AND oi.add_time < ".$time_end;
	}
	
	$where .= ' and og.goods_id = g.goods_id and c.color_id = gc.color_id and s.size_id = gs.size_id and og.size_id = gs.size_id and og.goods_id = gs.goods_id and og.goods_id = gc.goods_id and og.color_id = gc.color_id and og.order_id = oi.order_id and oi.order_status = '.OS_FINISH.' AND oi.pay_status = '.PS_PAYED_FINISH.' and oi.shipping_status = '.SS_SHIPPED;
	
	$sql = 'select og.goods_sn,og.goods_name,gc.color_name,c.color_code,gs.size_name,s.size_code,og.goods_price,sum(og.goods_number) as number_total, round(sum((goods_amount-discount+tax+shipping_fee+pay_fee+pack_fee+card_fee - bonus)/goods_amount * og.goods_price * og.goods_number),2) as money_total from '.$GLOBALS['os']->table('order_goods').' as og,'.$GLOBALS['os']->table('goods').' as g,'.$GLOBALS['os']->table('goods_size').' as gs,'.$GLOBALS['os']->table('goods_color').' as gc,'.$GLOBALS['os']->table('order_info').' as oi ,'.$GLOBALS['os']->table('color').' as c ,'.$GLOBALS['os']->table('size').' as s '.$where.' group by og.goods_id,og.size_id,og.color_id'.' order by og.goods_id';			
			
	$row = $GLOBALS['db']->getAll($sql);
	/* 格式化数据 */
	foreach ($row AS $key => $value){
		$number_sum += $value['number_total'];
		$money_sum  += $value['money_total'];
	}
	
	$result['list'] = $row;
	$result['number_sum'] = $number_sum;
	$result['money_sum']  = $money_sum;
	
	return $result;
}

?>